package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.EventStatusModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.XToast;

public class MatchActivity extends BaseActivity implements View.OnClickListener {

    private WebView webView;
    private ImageView imageView;
    private WebViewClient client = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }
    };
    private String cookieId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);
        initView();
        cookieId = GetLoginInfo.getLoginInfoInstance(this).readUser().getCookieId();
    }

    public void initView() {
        setTitleText("技能大赛培训");
        webView = findViewById(R.id.webView);
        imageView = findViewById(R.id.macth_image);
        imageView.setOnClickListener(this);
        requestNet();
    }

    public void requestNet() {
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.requestFocus();
        webView.setWebViewClient(client);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(Constants.Url.MACTHIMAGE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            //webView.getSettings().setJavaScriptEnabled(false);
            if (webView.getParent() != null) {
                ((ViewGroup) webView.getParent()).removeView(webView);
            }
            webView.removeAllViews();
            webView.clearHistory();
            webView.destroy();
            webView = null;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.macth_image:
                if (TextUtils.isEmpty(cookieId)){
                    XToast.showToast(this,"请您先登录");
                    IntentManager.goLoginActivity(this);
                    finish();
                    return;
                }
                requestPost();
                break;
        }
    }

    private void requestPost() {
        EventStatusModel.Body body = new EventStatusModel.Body();
        body.setPlayTime(0);
        HttpUtil.doPost(Constants.Url.TIME_COMPE, body, new HttpResponse(this, EventStatusModel.class) {
            @Override
            public void onResult(Object obj) {
                EventStatusModel data = (EventStatusModel) obj;
                EventStatusModel.Data data1 = data.getData();
                String status = data1.getStatus();
                if (!TextUtils.isEmpty(status) && status.equals("0")){
                    IntentManager.goEventListActivity(MatchActivity.this);
                }else if (!TextUtils.isEmpty(status) && status.equals("1")){
                    IntentManager.goCheckInActivity(MatchActivity.this);
                }
                finish();
            }
        });
    }
}
